package com.mojing.controller;

import com.mojing.entity.Book;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.zhipuai.ZhiPuAiChatOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/chatclient")
public class ZhipuChatClientController {

	private final ChatClient chatClient;

	// 构造器注入
	public ZhipuChatClientController(ChatClient.Builder builder) {
		this.chatClient = builder.build();
	}

//	@GetMapping("/simple")
//	public String simple(@RequestParam(name = "query") String query) {
//		SystemMessage systemMessage = new SystemMessage("你是一个很会教人的老师");
//		UserMessage userMessage = new UserMessage(query);
//		ZhiPuAiChatOptions zhiPuAiChatOptions = new ZhiPuAiChatOptions();
//		zhiPuAiChatOptions.setModel("glm-4.5");
//		zhiPuAiChatOptions.setTemperature(0.5);
//		zhiPuAiChatOptions.setMaxTokens(15536);
//		Prompt prompt = new Prompt(List.of(systemMessage, userMessage), zhiPuAiChatOptions);
//		// 调用模型
//		return chatClient.prompt(prompt)
//				.call()
//				.content();
//	}

	@GetMapping("/simple")
	public String simple(@RequestParam(name = "query") String query) {
		ZhiPuAiChatOptions zhiPuAiChatOptions = new ZhiPuAiChatOptions();
		zhiPuAiChatOptions.setModel("glm-4.5");
		zhiPuAiChatOptions.setTemperature(0.5);
		zhiPuAiChatOptions.setMaxTokens(15536);
		// 调用模型
		return chatClient.prompt()
				.system("你是一个很会教人的老师")
				.user(query)
				.options(zhiPuAiChatOptions)
				.call()
				.content();
	}

	@GetMapping("/entity")
	public Book entity(@RequestParam(name = "query") String query) {
		// 调用模型
		return chatClient.prompt()
				.user(query)
				.call().entity(Book.class);
	}

	@GetMapping("/stream")
	public Flux<String> stream(@RequestParam(name = "query") String query) {
		// 调用模型
		return chatClient.prompt()
				.user(query)
				.stream().content();
	}
}
